package com.zxc.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zxc.reggie.common.BaseContext;
import com.zxc.reggie.common.R;
import com.zxc.reggie.entity.ShoppingCart;
import com.zxc.reggie.service.ShoppingCartService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;


    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartService.query().eq("user_id", userId).orderByDesc("create_time").list();
        return R.success(list);
    }

    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {

        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        shoppingCart.setCreateTime(LocalDateTime.now());
        Long setmealId = shoppingCart.getSetmealId();
        Long dishId = shoppingCart.getDishId();

        ShoppingCart shoppingCartBase = shoppingCartService.query().eq("user_id", userId).eq(setmealId != null, "setmeal_id", setmealId).eq(dishId != null, "dish_id", dishId).one();

        if (shoppingCartBase != null) {
            Integer number = shoppingCartBase.getNumber();
            number++;
            shoppingCartService.update().eq("user_id", userId).eq(setmealId != null, "setmeal_id", setmealId).eq(dishId != null, "dish_id", dishId).setSql("number =" + number).update();
            return R.success("加一");
        } else {
            shoppingCartService.save(shoppingCart);
            return R.success("已加入购物车");
        }


    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody Map<String, String> map) {
        Long userId = BaseContext.getCurrentId();
        String setmealId = map.get("setmealId");
        String dishId = map.get("dishId");
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
        ShoppingCart shoppingCart = shoppingCartService.query().eq("user_id", userId).eq(setmealId != null, "setmeal_id", setmealId).eq(dishId != null, "dish_id", dishId).one();
        Integer number = shoppingCart.getNumber();
        number--;
        if (number <= 0) {
            shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        } else {
            shoppingCartService.update().eq("user_id", userId).eq(setmealId != null, "setmeal_id", setmealId).eq(dishId != null, "dish_id", dishId).setSql("number = " + number).update();
        }
        return R.success("移除成功");
    }

    @DeleteMapping("/clean")
    public R<String> clean() {
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        return R.success("清空成功");
    }
}
